#!/usr/bin/perl 
use strict;
use warnings;
use bytes;
use XML::Parser;
use File::Spec::Functions;
use File::Spec;

our %params = (
    working_dir         => '/home/xuqy/dmoz/',
    structure_rdf_file  => 'data/structure.clean',
);

$| =1;

chdir $params{working_dir} or die "$!";

my $p1 = new XML::Parser(
              Style => 'Stream',
              ErrorContext => 2,
		      ProtocolEncoding => 'UTF-8',
		      NoExpand => 1,
		      ParseParamEnt => 0);

my @pendingTag = ();
my ($srcPath, $link, $dstPath, $records);

$p1->parsefile($params{structure_rdf_file});

exit;

sub StartDocument {
  print "\nLoading structure records\n";
  return;
}

sub StartTag {
    no warnings;
  m/^<(\S+)[\s>].*/;
  my $tag = $1;
  unshift @pendingTag, $tag;
  if ($tag eq 'Topic') {
      $srcPath = $_{'r:id'};
  }
  elsif ($tag =~ /^symbolic/) {
      my $pair = $_{'r:resource'};
      my ($link, $dstPath) = split /:/, $pair;
      if (not &ignorable($srcPath) and not &ignorable($dstPath) and -d $srcPath and -d $dstPath) {
        my $src = catfile($srcPath, "$link@");
        my $dst = File::Spec->abs2rel(File::Spec->rel2abs($dstPath), File::Spec->rel2abs($srcPath));
        if (symlink($dst, $src)) {
            $records++;
            print "Record $records\n" unless $records % 1000;
        }
      }
  }
  elsif ($tag eq 'Alias' || $pendingTag[1] eq 'Alias' || $pendingTag[1] eq 'Topic' || $tag eq 'RDF') { return; }
  else { print "Warning: Unhandled Tag [$tag] (Pending [$pendingTag[1]])\n"; }
}

sub EndTag {
  shift @pendingTag;
}

sub Text {
  return;
}

sub PI {
  return;
}

sub EndDocument {
  print "$records symbolic link made.\n";
  return;
}

sub ignorable {
    my $topic = shift;
    $topic =~ m{^Top/(?:Adult|Kids_and_Teens|World|Regional)};
}
